import Vue from 'vue'
import VueRouter from 'vue-router'

import Register from '../views/users/Register.vue'
import Login from '../views/users/Login.vue'

import Admin from '../views/admin/admin.vue'
import Home from '../views/admin/main/home/Home.vue'

import User from 'views/admin/main/user/User.vue'
import Product from 'views/admin/main/product/Product.vue'
import Pcreate from 'views/admin/main/product/Create.vue'




const Order = () => import('views/admin/main/order/Order.vue')

Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'Home',
    component: Login,
},
{
    path: '/login',
    component: Login
},
{
    path: '/admin',
    component: Admin,
    redirect: '/admin/home',
    children: [{
        path: 'home',
        component: Home,
        meta: [
            { name: '首页' , url: '/admin'},
        ],
    },
    {
        path: 'user',
        component: User,
        meta: [
            { name: '首页' },
            { name: '用户', url: '/admin/user' }
        ],
    },
    {
        path: 'product',
        component: Product,
        meta: [
            { name: '首页' },
            { name: '商品列表', url: '/admin/project' }
        ],
    },
    {
        path: 'pcreate',
        component: Pcreate,
        meta: [
            { name: '首页' },
            { name: '添加商品', url: '/admin/project' }
        ],
    },
    {
        path: 'order',
        component: Order,
        meta: [
            { name: '首页' },
            { name: '订单列表', url: '/admin/order' }
        ],
    }
    ]
},

{
    path: '/lock',
    component: () =>
        import( /* webpackChunkName: "about" */ '../views/Lock.vue')
},
{
    path: '*',
    name: 'Nofind',
    component: () => import('../views/404.vue')
}
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

router.beforeEach((newData, oldData, next) => {
    setTimeout(() => {
        let token = localStorage.getItem('token')
        if (newData.path != '/login' && !token) {
            next({ path: '/login' })
        } else {
            next()
        }
    }, 500)
})



import NProgress from 'nprogress'
import 'nprogress/nprogress.css'


router.beforeEach((to, from, next) => {
    NProgress.start()
    next()
})
router.afterEach(() => {
    NProgress.done()
})
NProgress.configure({ ease: 'ease', speed: 500 });
export default router